package com.xxx.modules.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.modules.entity.Email;
import com.xxx.modules.service.EmailService;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@RestController
@RequestMapping("/common/email")
@Api(tags="email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/sendEmail")
    public Object sendEmail(@RequestBody Email email) {
        // 发送邮件
        // map
        Properties pro = new Properties();
        pro.put("mail.transport.protocol","smtp");
        pro.put("mail.smtp.host","smtp.qq.com");
        // 建立连接
        Session session = Session.getInstance(pro);
        // 模拟对象
        try {
            // 设置邮件内容
            MimeMessage message = new MimeMessage(session);
            String from = "2799594324@qq.com"; // 发件人的邮箱地址
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getAddressee()));
            message.setSubject(email.getSubject());
            message.setText(email.getContent());
            // 发送邮件
            Transport transport = session.getTransport();
            transport.connect("2799594324@qq.com","uellzpskzzzqdfab");
            transport.sendMessage(message,message.getAllRecipients());
            return ResultUtil.success(1,"成功",null);
        } catch (MessagingException e) {
            e.printStackTrace();
            return ResultUtil.error(2,"失败");
        }
    }

    @GetMapping("/getEmailInfo")
    @ApiOperation("获取所有公告")
    public Result getEmailInfo(Email email, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Email> data = emailService.selectEmailInfo(email,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }

    /**
     * 保存email
     * @param email
     * @return
     */
    @PostMapping("/saveEmailInfo")
    @ApiOperation("保存公告")
    public Result saveEmailInfo(@RequestBody Email email){
        emailService.saveEmailInfo(email);

        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新email
     * @param email
     * @return
     */

    @PutMapping("/updateEmailInfo")
    @ApiOperation("更新")
    public Result updateEmailInfo(@RequestBody Email email){
        emailService.updateEmailInfo(email);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除email
     * @param id
     * @return
     */
    @DeleteMapping("/delEmailInfo")
    public Result delEmailInfo(Integer id){
        emailService.delEmailInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合删除notice
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchEmailInfo")
    public Result delBatchEmailInfo(String idList){
        emailService.delBatchEmailInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }

}




